package com.ramos.kuaixiao.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.ramos.kuaixiao.App;
import com.ramos.kuaixiao.MyView.CartDialog;
import com.ramos.kuaixiao.MyView.SelfDialog;
import com.ramos.kuaixiao.R;
import com.ramos.kuaixiao.db.BrandInfoDB;
import com.ramos.kuaixiao.entity.BrandInfo;
import com.ramos.kuaixiao.entity.CartInfo;
import com.ramos.kuaixiao.utils.StringUtil;

import java.util.ArrayList;
import java.util.List;

import jp.wasabeef.glide.transformations.CropSquareTransformation;

public class CartAdapter extends BaseExpandableListAdapter {
    private Context mContext;// 上下文
    private List<CartInfo> mParents = new ArrayList<>();// 数据源
    private List<BrandInfo> tasklist = new ArrayList<>();
    UpdateCarListener updateListener;

    public CartAdapter(Context context, List<CartInfo> parents) {
        this.mContext = context;
        this.mParents = parents;
        for (CartInfo data : mParents) {
            Log.d("parentAdapter", "ParentAdapter " + data.getStoreNum());
        }
    }

    public void refresh(List<CartInfo> tasks) {
        this.mParents = tasks;
        notifyDataSetChanged();
    }

    // 获得某个父项的某个子项
    @Override
    public BrandInfo getChild(int groupPosition, int childPosition) {
        if (mParents.get(groupPosition).getBrandInfos() == null) {
            return null;
        } else {
            return mParents.get(groupPosition).getBrandInfos().get(childPosition);
        }
    }

    // 获得某个父项的某个子项的id
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    // 获得某个父项的子项数目
    @Override
    public int getChildrenCount(int groupPosition) {
        return mParents.get(groupPosition).getBrandInfos() != null ? mParents.get(groupPosition).getBrandInfos().size() : 0;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isExpanded, View convertView,
                             ViewGroup parent) {

        final BrandInfo child = getChild(groupPosition, childPosition);
        if (child != null) {
            CartAdapter.ViewHolder holder = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item_cart_goods, null);
                holder = new CartAdapter.ViewHolder();
                holder.name = (TextView) convertView.findViewById(R.id.tv_goods_title);
                holder.price = (TextView) convertView.findViewById(R.id.tv_goods_price);
                //holder.encode = (TextView) convertView.findViewById(R.id.brand_encode);
                holder.store = (TextView) convertView.findViewById(R.id.tv_cart_amount);
                holder.icon = (ImageView) convertView.findViewById(R.id.sdv_goods_img);
                holder.delete = (ImageView) convertView.findViewById(R.id.iv_cart_delete);
                convertView.setTag(holder);
            } else {
                holder = (CartAdapter.ViewHolder) convertView.getTag();
            }
            holder.name.setText(child.getShop_Name());
            holder.price.setText("￥" + child.getPrice() + "/支");
            //holder.encode.setText("编:" + tasklist.get(position).getEncode());
            holder.store.setText(child.getNum_Box() + "箱 " + child.getNum_Single() + "支");
            //Uri uri = Uri.parse(tasklist.get(position).getIcon());
            if(!StringUtil.isEmpty(child.getIcon())) {
                Glide.with(mContext).load(child.getIcon()).dontAnimate().bitmapTransform(new CropSquareTransformation(mContext)).diskCacheStrategy(DiskCacheStrategy.ALL)
                        .into(holder.icon);
            } else {
                holder.icon.setImageResource(R.drawable.no_img);
            }
            holder.delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final SelfDialog builder = new SelfDialog(mContext);
                    builder.setTitle("删除商品");
                    builder.setMessage("是否删除该商品");
                    builder.setNoOnclickListener("取消", new SelfDialog.onNoOnclickListener() {
                        @Override
                        public void onNoClick() {
                            builder.dismiss();
                        }
                    });
                    builder.setYesOnclickListener("确定", new SelfDialog.onYesOnclickListener() {
                        @Override
                        public void onYesClick() {
                            BrandInfoDB user = new BrandInfoDB();
                            user.deleteShop(child);
                            tasklist.clear();
                            tasklist = user.findAllShop(App.mUserId);
                            mParents = user.getAllCart(tasklist);
                            refresh(mParents);
                            notifyListeners(tasklist);
                            builder.dismiss();
                        }
                    });
                    builder.show();

                }
            });
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final CartDialog builder = new CartDialog(mContext, child);
                    //builder.setTitle("购物车");
                    builder.setNoOnclickListener("取消", new CartDialog.onNoOnclickListener() {
                        @Override
                        public void onNoClick() {
                            builder.dismiss();
                        }
                    });
                    builder.setYesOnclickListener("确定", new CartDialog.onYesOnclickListener() {
                        @Override
                        public void onYesClick() {
                            int i = builder.modifyCart();
                            if (i == 1) {
                                tasklist.clear();
                                BrandInfoDB user = new BrandInfoDB();
                                tasklist = user.findAllShop(App.mUserId);
                                mParents = user.getAllCart(tasklist);
                                refresh(mParents);
                                notifyListeners(tasklist);
                                builder.dismiss();
                            }
                        }
                    });
                    builder.show();
                }
            });
            return convertView;
        }
        return convertView;
    }

    static class ViewHolder {
        TextView name;
        TextView price;
        TextView store;
        ImageView icon;
        ImageView delete;
    }

    class GroupHolder {
        TextView tvWare;
        TextView tvMoney;
    }

    // 获得某个父项
    @Override
    public Object getGroup(int groupPosition) {
        return mParents.get(groupPosition);
    }

    // 获得父项的数量
    @Override
    public int getGroupCount() {
        return mParents != null ? mParents.size() : 0;
    }

    // 获得某个父项的id
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        CartAdapter.GroupHolder groupholder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_cart, parent, false);
            groupholder = new CartAdapter.GroupHolder();
            groupholder.tvWare = (TextView) convertView.findViewById(R.id.tvWare);
            groupholder.tvMoney = (TextView) convertView.findViewById(R.id.tvMoney);
            convertView.setTag(groupholder);
        } else {
            groupholder = (CartAdapter.GroupHolder) convertView.getTag();
        }
        groupholder.tvWare.setText(mParents.get(groupPosition).getWarehouse() + "(" + mParents.get(groupPosition).getStoreNum() + ")");
        groupholder.tvMoney.setText("￥" + App.df.format(Double.valueOf(mParents.get(groupPosition).getPrice())));
        return convertView;
    }


    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private void notifyListeners(List<BrandInfo> mListGoods) {
        updateListener.update(mListGoods);
    }

    public interface UpdateCarListener {
        void update(List<BrandInfo> mListGoods);
    }

    public void setUpdateCarListener(UpdateCarListener updateListener) {
        this.updateListener = updateListener;
    }
}